﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Collections;
using PRScreenWithKinect.Model;

namespace PRScreenWithKinect
{
	/// <summary>
	/// Interaction logic for UserControl1.xaml
	/// </summary>
	public partial class Location : UserControl,UserControlInterfaceForGesture
	{
        enum Catagory
        {
            Teacher=1,Office=2,Classroom=3,Lab=4
        }


        private ArrayList controllerElement;
        private Grid prevMenu;
        private UIElement hitElement = null;
        private UIElement hittedElement = null;
        private Double focusOpacity = 0.7, defaultOpacity = 0.3;
        private SolidColorBrush focusColor = Brushes.Orange, defaultColor = Brushes.White;
        private int pageIndex = 0;
        private ListBox[] ar;
        private int mainController;

		public Location()
		{
			this.InitializeComponent();
            Content_Zoom.Visibility = Visibility.Hidden;

            // Add Menu to controllerElement ;s
            controllerElement = new ArrayList();
            controllerElement.Add(Menu1);
            controllerElement.Add(Menu2);
            controllerElement.Add(Menu3);
            controllerElement.Add(Menu4);
            controllerElement.Add(CloseZoom);

            mainController = controllerElement.Count;


            // Set Hit Element = menu1 ;
            hittedElement = Menu1;


            /// Load Data with Catagory All 
            loadDataFromJSON(Catagory.Teacher);
		}

		private void Grid_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
		{
			// TODO: Add event handler implementation here.
			Content_Zoom.Visibility = Visibility.Visible;
            ScrollingContent.Visibility = Visibility.Hidden;
		}

		private void CloseZoom_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
		{
			// TODO: Add event handler implementation here.
            Content_Zoom.Visibility = Visibility.Hidden;
            ScrollingContent.Visibility = Visibility.Visible;
		}
        private void loadDataFromJSON(Catagory c)
        {
            //// Remove all data from controllerElement 
            if (prevMenu != null)
            {
                pageIndex = 0;
                ((Path)prevMenu.Children[0]).Fill = defaultColor;
                controllerElement.RemoveRange(mainController, controllerElement.Count - mainController);
                viewer.Children.RemoveRange(0, viewer.Children.Count);
            }
            if (c == Catagory.Teacher)
            {
                prevMenu = Menu1;

            }
            else if (c == Catagory.Office)
            {
                prevMenu = Menu2;
            }
            else if (c == Catagory.Classroom)
            {
                prevMenu = Menu3;
            }
            else if (c == Catagory.Lab)
            {
                prevMenu = Menu4;
            }

            ((Path)prevMenu.Children[0]).Fill = focusColor;

            //ar = new ListBox[3];
            //ar[0] = new ListBox();
            //ar[1] = new ListBox();
            //ar[2] = new ListBox();
            //   int numOfItem =  0 ;
            int w_list = 230;
            int h_list = 320;
            Thickness _margin = new Thickness(0, 0,4, 0);
         
            DataFactory  dataFactory = DataFactory.Instance;
            List<LocationModel> locations = dataFactory.getLocationByCatagory((int)c);

            int locationPerColumn = 5;
            int numOfData = locations.Count;
            int numOfColumn =(int)Math.Ceiling(numOfData / (float)locationPerColumn);
            int numOfPage = (int)Math.Ceiling(numOfColumn / 2.0);

            for (int i = 0; i < numOfColumn;i++ )
            {
                ListBox l = new ListBox();
                l.Width = w_list;
                l.Height = h_list;
                l.AlternationCount = 2;
                l.Margin = _margin;

                //LocationModel[] loc = new LocationModel[5];


                for (int j = 0; j < 5; j++)
                {
                    //loc[j] = new LocationModel();
                    //loc[j].roomNumber = Convert.ToString(j);
                    //loc[j].name = "M03";
                    //loc[j].imagePath = "test.jpg";
                    //loc[j].hitCounter = 34;
                    //loc[j].catagory = "ห้องปฏิบัติการ";

                    int location_index = i * locationPerColumn + j;
                    if (location_index < locations.Count)
                    {
                        ListBoxItem it = new ListBoxItem();
                        it.Template = (ControlTemplate)this.Resources["MyDataTemplate"];
                        it.DataContext = locations[location_index];
                        it.Margin = new Thickness(0, 4, 0, 0);

                        l.Items.Add(it);
                        controllerElement.Add(it);
                    }
                    else
                    {
                        break;
                    }
              


                }

                l.BorderThickness = new Thickness(0);
                l.SetValue(ScrollViewer.HorizontalScrollBarVisibilityProperty, ScrollBarVisibility.Disabled);
                l.SetValue(ScrollViewer.VerticalScrollBarVisibilityProperty, ScrollBarVisibility.Disabled);

                //l.ItemsSource = news;
                //   l.Background = Brushes.Yellow;

                viewer.Children.Add(l);


                //   Console.WriteLine("News(Constuct) : " + viewer.Children.Count);


            }

            if (numOfColumn % 2 != 0)
            {
                Image b = new Image();
                b.Width = w_list;
                b.Height = h_list;
                b.Margin = _margin;
                viewer.Children.Add(b);
            }
            
            createPageBullet(countPage());
            //  Console.WriteLine("News(LoadData) : " + c);
        }
        private int countPage()
        {
            int i = Convert.ToInt16( Math.Ceiling(viewer.Children.Count / 2.0));
            return i;
        }
        public void finishHold()
        {
            if (hitElement.Equals(Menu1))
            {
                loadDataFromJSON(Catagory.Teacher);
            }
            else if (hitElement.Equals(Menu2))
            {
                loadDataFromJSON(Catagory.Office);
            }
            else if (hitElement.Equals(Menu3))
            {
                loadDataFromJSON(Catagory.Classroom);
            }
            else if (hitElement.Equals(Menu4))
            {
                loadDataFromJSON(Catagory.Lab);
            }
            else if (hitElement.GetType() == typeof(ListBoxItem))
            {
              //  int itemIndex = controllerElement.IndexOf(hitElement);
                zoomContent(hitElement);
            }
            else if (hitElement.Equals(CloseZoom))
            {
                closeZoom();
            }
         //   Console.WriteLine("Type Hit Element : " + hitElement.GetType());
            hittedElement = hitElement;
            hitElement = null;
        }


        public void swipeLeft()
        {
            doSwipe(Swipe.Left);
        }

        public void swipeRight()
        {
            doSwipe(Swipe.Right);
        }

 
        public void doSwipe(Swipe s)
        {

            if (s == Swipe.Left && pageIndex!= (countPage()-1))
            {
                pageIndex++;
                pageBulletStack.Children[pageIndex].Opacity = focusOpacity;
                pageBulletStack.Children[pageIndex - 1].Opacity = defaultOpacity;
          
            }
            else if(s== Swipe.Right && pageIndex!=0)
            {
       
                pageIndex--;
                pageBulletStack.Children[pageIndex].Opacity = focusOpacity;
                pageBulletStack.Children[pageIndex +1].Opacity = defaultOpacity;

            }
            scrollView.ScrollToPosition(scrollView.Width * pageIndex, 0);
           
            //Console.WriteLine("News(doSwipe) : " + pageIndex);
         
        }

   public void zoomContent(UIElement obj)
        {
            ListBoxItem li = (ListBoxItem)obj;
         //   ListBox b = (ListBox)viewer.Children[pageIndex];
         //   NewsModel openNews = (NewsModel)((ListBoxItem)b.Items[i]).DataContext;
            LocationModel loc = (LocationModel)li.DataContext;
         //   Console.WriteLine("Zoom Content i : " +openNews.date);
            _headZoom.Text = loc.name;
            _subHeadZoom.Text = string.Format("{0} |  {1}", loc.name, loc.catagory);
         //   _contentZoom.Text = openNews.content;

            Content_Zoom.Visibility = Visibility.Visible;
            scrollView.Visibility = Visibility.Hidden;
            pageBulletStack.Visibility = Visibility.Hidden;
        }
        public void closeZoom()
        {
            Content_Zoom.Visibility = Visibility.Hidden;
           scrollView.Visibility = Visibility.Visible;
           pageBulletStack.Visibility = Visibility.Visible;
        }
        private void createPageBullet(int n)
        {
            pageBulletStack.Children.RemoveRange(0, pageBulletStack.Children.Count);
            for (int i = 0; i < n; i++)
            {
                Ellipse myEllipse = new Ellipse();

                // Create a SolidColorBrush with a red color to fill the 
                // Ellipse with.

                myEllipse.Fill = Brushes.Gray;
                if (i == 0)
                {
                    myEllipse.Opacity = focusOpacity;
                }
                else
                {
                    myEllipse.Opacity = defaultOpacity;
                }



                // Set the width and height of the Ellipse.
                myEllipse.Width = 20;
                myEllipse.Height = 20;
                myEllipse.Margin = new Thickness(5, 0, 0, 0);
                //   Console.WriteLine("Add Bullet");
                pageBulletStack.Children.Add(myEllipse);
            }


        }


        public bool hitTest(Point p, Window w)
        {
            Boolean isHit = false;

            foreach (UIElement c in controllerElement)
            {
                Point RhandRelativePoint = c.TransformToAncestor(w).Transform(new Point(0, 0));
                Point delta = new Point(p.X - RhandRelativePoint.X, p.Y - RhandRelativePoint.Y);
                if (VisualTreeHelper.HitTest(c, delta) != null)
                {
                    if (c.Equals(hitElement) && !c.Equals(hittedElement))
                    {
                        if (c.GetType() == typeof(ListBoxItem) || c.Equals(CloseZoom))
                        {
                            if (c.GetType() == typeof(ListBoxItem) && scrollView.Visibility == Visibility.Visible)
                            {
                                return true;
                            }

                            if (c.Equals(CloseZoom) && Content_Zoom.Visibility == Visibility.Visible)
                            {
                                return true;
                            }
                            return false;
                        }
                        else 
                        {
                            return true;
                        }
                    }
                    else
                    {

                        hitElement = c;
                        return false;

                    }
                }
            }




            return isHit;
        }
    }
}